//
//  TKYSafetyDescribeCell.m
//  StationHouseHelper
//
//  Created by apple on 2018/6/29.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYSafetyDescribeCell.h"
@interface TKYSafetyDescribeCell()<UITextViewDelegate>


@property (nonatomic, strong)UITextView *describeTextView;


@end
@implementation TKYSafetyDescribeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.describeTextView];
        [self.describeTextView addSubview:self.placeholderLabel];
        
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];

        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).with.offset(20);
            make.top.equalTo(self.contentView).with.offset(5);
            //make.right.equalTo(self.contentView).with.offset(-10);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(100);
        }];
        
        [self.describeTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).with.offset(20);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
            make.right.equalTo(self.contentView).with.offset(-10);
            make.bottom.equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(80);
        }];
        
        [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.describeTextView).with.offset(0);
            make.top.equalTo(self.describeTextView.mas_top).offset(5);
            make.right.equalTo(self.self.describeTextView).with.offset(0);
           // make.bottom.equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(20);
        }];
        
         [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    return self;
}


- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"问题描述";
        _titleLabel.font = Font(12);
    }
    return _titleLabel;
}

- (UITextView *)describeTextView{
    if (_describeTextView == nil) {
        _describeTextView = [[UITextView alloc]init];
      //  _describeTextView.backgroundColor = [UIColor redColor];
        _describeTextView.delegate = self;
    }
    return _describeTextView;
}

- (UILabel *)placeholderLabel{
    if (_placeholderLabel == nil) {
        _placeholderLabel = [[UILabel alloc]init];
        _placeholderLabel.font = Font(12);
        _placeholderLabel.textColor = kRGBColor(211, 211, 211, 1);
       // _placeholderLabel.text = @"请输入问题描述";
    }
    return _placeholderLabel;
}


-(void)textViewDidChange:(UITextView *)textView
{
    self.placeholderLabel.text = @"";
    self.placeholderLabel.hidden = YES;
    //实时显示字数
//    self.stringlenghtLab.text = [NSString stringWithFormat:@"%ld/100",(long)textView.text.length];
    
    //字数限制
    if (textView.text.length >= 100) {
        textView.text = [textView.text substringToIndex:100];
    }
    
    //取消按钮点击权限，并显示文字
    if (textView.text.length == 0) {
        self.placeholderLabel.hidden = NO;
        
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        
        [self.describeTextView resignFirstResponder];
        
        return NO;
    }
    
    if (self.describeTextBlock) {
        self.describeTextBlock(text);
    }
    
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if (self.describeTextBlock) {
        self.describeTextBlock(textView.text);
    }
    return YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
